package com.xbongbong.saas.script.tempenum;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.enums.DateEnum;
import com.xbongbong.paas.help.FormDataFormExplainHelper;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FormulaPoJo;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.field.LinkDetailPojo;
import com.xbongbong.paas.field.SerialNumberPoJo;
import com.xbongbong.pro.enums.AmountFlagEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.paas.field.SaasParticularAttributePoJo;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.ForbiddenSettingEnum;
import com.xbongbong.saas.enums.RedundantTemplateTypeEnum;
import com.xbongbong.saas.enums.ReturnedPurchaseStatusEnum;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.enums.ShowTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ReturnedPurchaseEnum;
import com.xbongbong.saas.enums.dictionary.ReturnedPurchaseTypeEnum;
import com.xbongbong.saas.enums.dictionary.ReturnedStatusEnum;
import com.xbongbong.saas.script.help.InitExplainsHelper;
import com.xbongbong.sys.enums.RoleEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 采购退货单
 * @author Jian.Wang
 * @date 2019/1/18 14:42
 * @since v1.0
 * @version v1.0
 */
@Getter
public enum TempReturnedPurchaseEnum {
    /**
     * 采购退货
     */

    BASE_INFO(ReturnedPurchaseEnum.BASE_INFO),
    REFUND_INFO(ReturnedPurchaseEnum.REFUND_INFO),
    SHEET_NO(ReturnedPurchaseEnum.SHEET_NO),
    LINK_SUPPLIER(ReturnedPurchaseEnum.LINK_SUPPLIER),
    PURCHASE_SHEET_ID(ReturnedPurchaseEnum.PURCHASE_SHEET_ID),
    MANAGER_ID(ReturnedPurchaseEnum.MANAGER_ID),
    TIME(ReturnedPurchaseEnum.TIME),
    PRODUCTS(ReturnedPurchaseEnum.PRODUCTS),
    TOTAL_MONEY(ReturnedPurchaseEnum.TOTAL_MONEY),
    RETURNED_STATUS(ReturnedPurchaseEnum.RETURNED_STATUS),
    MEMO(ReturnedPurchaseEnum.MEMO),

    SYSTEM_INFO(ReturnedPurchaseEnum.SYSTEM_INFO),
    CREATOR_ID(ReturnedPurchaseEnum.CREATOR_ID),
    ADD_TIME(ReturnedPurchaseEnum.ADD_TIME),
    UPDATE_TIME(ReturnedPurchaseEnum.UPDATE_TIME),

    PAY_SHEETS(ReturnedPurchaseEnum.PAY_SHEETS),
    RETURNED_PURCHASE_AMOUNT(ReturnedPurchaseEnum.RETURNED_PURCHASE_AMOUNT),
    INVOICES(ReturnedPurchaseEnum.INVOICES),

    STATUS(ReturnedPurchaseEnum.STATUS),
    TYPE(ReturnedPurchaseEnum.TYPE),
    OUTSTOCK_ID(ReturnedPurchaseEnum.OUTSTOCK_ID),
    PURCHASE_SHEET_ID_LINKED_TEXT(ReturnedPurchaseEnum.PURCHASE_SHEET_ID_LINKED_TEXT),
    SUPPLIER_ID(ReturnedPurchaseEnum.SUPPLIER_ID),
    LINK_SUPPLIER_HIDE(ReturnedPurchaseEnum.LINK_SUPPLIER_HIDE),
    PAY_SHEETS_LINKED_TEXT(ReturnedPurchaseEnum.PAY_SHEETS_LINKED_TEXT),
    INVOICES_LINKED_TEXT(ReturnedPurchaseEnum.INVOICES_LINKED_TEXT),
    ;
    private ReturnedPurchaseEnum anEnum;

    TempReturnedPurchaseEnum(ReturnedPurchaseEnum returnedPurchaseEnum) {
        this.anEnum = returnedPurchaseEnum;
    }

    public static List<TempReturnedPurchaseEnum> getAllEnum() {
        List<TempReturnedPurchaseEnum> list = new ArrayList<>();
        Collections.addAll(list, values());
        return list;
    }

    /**
     * 初始化采购退货单的默认编号
     *
     * @param fieldAttrEntity 字段解释实体--saas专用
     */
    public static void initSheetNo(FieldAttrEntity fieldAttrEntity){
        LinkDetailPojo linkDetailPojo = new LinkDetailPojo();
        linkDetailPojo.setBusinessType(XbbRefTypeEnum.RETURNED_PURCHASE.getCode());
        linkDetailPojo.setSubBuisenssType(XbbRefTypeEnum.RETURNED_PURCHASE.getCode());
        linkDetailPojo.setSaasMark(SaasMarkEnum.SAAS.getCode());
        linkDetailPojo.setAttr(SHEET_NO.getAnEnum().getAttr());
        fieldAttrEntity.setLinkDetail(linkDetailPojo);
        fieldAttrEntity.setNoRepeat(1);
        SerialNumberPoJo serialNumberPoJo = InitExplainsHelper.setSerialNumber(SerialNumberEnum.RETURNED_PURCHASE_NO);
        fieldAttrEntity.setSerialNumber(serialNumberPoJo);
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 初始化采购退货单的默认编号
     *
     * @param fieldAttrEntity 字段解释实体--saas专用
     */
    public static void initTime(FieldAttrEntity fieldAttrEntity){
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultType(DateEnum.TODAY.getValue());
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setDateType("yyyy-MM-dd");
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 初始化采购退货单的状态
     * @param fieldAttrEntity
     */
    public static void initStatus(FieldAttrEntity fieldAttrEntity){
//        List<ItemPoJo> itemPoJos = new ArrayList<>();
//        for(ReturnedPurchaseStatusEnum item : ReturnedPurchaseStatusEnum.values()) {
//            if (Objects.nonNull(item.getStatus())) {
//                ItemPoJo itemPoJo = new ItemPoJo();
//                itemPoJo.setValue(item.getStatus());
//                itemPoJo.setText(item.getMemo());
//                itemPoJos.add(itemPoJo);
//            }
//        }
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(ReturnedPurchaseStatusEnum.class, fieldAttrEntity.getFieldType(),"memo","status"));
    }

    public static void initType(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(ReturnedPurchaseTypeEnum.class, fieldAttrEntity.getFieldType()));
    }

    /**
     * 初始化关联采购合同
     *
     * @param fieldAttrEntity 字段解释实体--saas专用
     */
    public static void initPurchaseSheetId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.PURCHASE.getCode());
        fieldAttrEntity.setLinkedText(PURCHASE_SHEET_ID_LINKED_TEXT.getAnEnum().getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        saasParticularAttributePoJo.setRelyType("base");
        saasParticularAttributePoJo.setRelyMode("standard");
        saasParticularAttributePoJo.setRelySubModel("returnPurchase");
        saasParticularAttributePoJo.setRelyAttr(LINK_SUPPLIER.anEnum.getAttr());
        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.SUPPLIER.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 选择产品
     * @param fieldAttrEntity 字段解释实体--saas专用
     */
    public static void initProducts(FieldAttrEntity fieldAttrEntity) {
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setRelyType("relied");
        saasParticularAttributePoJo.setRelyMode("standard");
        saasParticularAttributePoJo.setRelySubModel("returnPurchase");
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setIsForbidden(0);
    }

    public static void initTotalMoney(FieldAttrEntity fieldAttrEntity) {
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "SUM({self." + ReturnedPurchaseEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.SUBTOTAL.getAttr() + "})";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + ReturnedPurchaseEnum.PRODUCTS.getAttr() + "." + SelectProductEnum.SUBTOTAL.getAttr(), "关联产品.售价小计");
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setIsForbidden(0);
    }

    public static void initCreatorId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }

    public static void initAddTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }
    public static void initUpdateTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }

    public static void initManagerId(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setMultiple(0);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setRequired(1);
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setAdd(1);
        fieldAttrEntity.setAddAdvanceEnable(1);
        fieldAttrEntity.setShowAdd(1);
        fieldAttrEntity.setRemove(1);
        fieldAttrEntity.setRemoveAdvanceEnable(1);
        fieldAttrEntity.setShowRemove(1);
        fieldAttrEntity.setAddRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.PURCHASE_MANAGER));
        fieldAttrEntity.setRemoveRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.PURCHASE_MANAGER));
    }

    public static void initLinkSupplier(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.SUPPLIER.getCode());
        fieldAttrEntity.setLinkedText(LINK_SUPPLIER_HIDE.getAnEnum().getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);

        saasParticularAttributePoJo.setRelyType("base");
        saasParticularAttributePoJo.setRelyMode("customer");
        saasParticularAttributePoJo.setRelySubModel("supplier");

        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 初始化红冲付款单
     * @author zhouwq
     * @date 2020/2/19 14:16
    */
    public static void initPaySheets(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(XbbRefTypeEnum.PAY_SHEET.getCode());
        fieldAttrEntity.setLinkedText(PAY_SHEETS_LINKED_TEXT.anEnum.getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setRelyAttr(PURCHASE_SHEET_ID.anEnum.getAttr());
        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.PURCHASE.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
    }

    /**
     * 初始化关联发票
     * @author zhouwq
     * @date 2020/2/19 14:16
     */
    public static void initInvoices(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(XbbRefTypeEnum.PURCHASE_INVOICE.getCode());
        fieldAttrEntity.setLinkedText(INVOICES_LINKED_TEXT.anEnum.getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setRelyAttr(PURCHASE_SHEET_ID.anEnum.getAttr());
        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.PURCHASE.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
    }

    /**
     * 初始化合计退款金额
     * @author zhouwq
     * @date 2020/2/19 14:16
     */
    public static void initReturnedPurchaseAmount(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(BasicConstant.ZERO);
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setSummaryFlag(1);
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setForbiddenSettingList(Collections.singletonList(ForbiddenSettingEnum.DISABLE_DEFAULT.getAlias()));
    }

    public static void initReturnedStatus(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setIsDictionary(1);
        List<ItemPoJo> itemPoJos = InitExplainsHelper.setCombItemByEnum(ReturnedStatusEnum.class, fieldAttrEntity.getFieldType());
        List<String> shipStatusCodeList = Arrays.asList(ReturnedStatusEnum.UNRETURNED.getName(), ReturnedStatusEnum.DELIVERED.getName(), ReturnedStatusEnum.RETURNED.getName(), ReturnedStatusEnum.CLOSED.getName());
        for (ItemPoJo itemPoJo: itemPoJos) {
            itemPoJo.setNoDel(1);
            itemPoJo.setNoEdit(1);
            if (!shipStatusCodeList.contains(itemPoJo.getText())) {
                itemPoJo.setShowType(ShowTypeEnum.NONE.getCode());
                itemPoJo.setEditHide(1);
            } else {
                if (Objects.equals(itemPoJo.getText(), ReturnedStatusEnum.UNRETURNED.getName())) {
                    itemPoJo.setChecked(true);
                }
            }
        }
        List<String> settingList = Arrays.asList(ForbiddenSettingEnum.DISABLE_OPTION_RELY.getAlias(), ForbiddenSettingEnum.DISABLE_SELF_DEFINE.getAlias());
        fieldAttrEntity.setForbiddenSettingList(settingList);
        fieldAttrEntity.setItemBanDefault(1);
        fieldAttrEntity.setItems(itemPoJos);
    }
}
